package com.king.reference.phantom;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

import com.king.reference.CheckRefQueue;
import com.king.reference.MyObject;

/**
 * @author taomk
 * @version 1.0
 * @since 15-1-27 下午10:47
 */
public class PhantomReferenceTest {

	public static void main(String [] args) throws InterruptedException {
		MyObject obj = new MyObject();
		ReferenceQueue<MyObject> phantomQueue = new ReferenceQueue<>();

		PhantomReference<MyObject> phantomObjRef = new PhantomReference<>(obj, phantomQueue);
		System.out.println("Phantom Get: " + phantomObjRef.get());

		new CheckRefQueue(phantomQueue).start();
		obj = null;

		Thread.sleep(1000);
		int i = 1;
		while (true) {
			System.out.println("第" + i++ + "次GC");
			System.gc();
			Thread.sleep(1000);
		}
	}
}
